package hutech.ksv.dao;

import hutech.ksv.entity.ThongTinNhaTroEntity;
import hutech.ksv.entity.ThongTinTaiKhoanAdmin;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class DuyetNhaTroDao implements Serializable {

	/**
	 * 
	 */
	
	@Autowired
	DataSource dataSource;
	
	private static final long serialVersionUID = 1L;
		
	public List<ThongTinNhaTroEntity> getTatCacThongTinNhaTro() {
		String query = "SELECT * FROM `taikhoancnt` as tkcnt "
				+ "INNER JOIN `chunhatro` AS cnt ON (`cnt`.`MaCNT` = `tkcnt`.`MaCNT`) "
				+ "INNER JOIN `nhatro` AS nt ON (`nt`.`TenTKCNT` = `tkcnt`.`TenTKCNT`) "
				+ "WHERE `nt`.`TrangThaiDuyetNT` = 0";
		List<ThongTinNhaTroEntity> listThongTinNhaTroEntity = new ArrayList<ThongTinNhaTroEntity>();

		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = dataSource.getConnection();
			preparedStatement = connection.prepareStatement(query);
			ResultSet rs = preparedStatement.executeQuery();
			while (rs.next()) {
				ThongTinNhaTroEntity thongTinNhaTro = new ThongTinNhaTroEntity();
				thongTinNhaTro.setTenTKCNT(rs.getString("TenTKCNT"));
				thongTinNhaTro.setMaCNT(rs.getInt("MaCNT"));
				thongTinNhaTro.setTenCNT(rs.getString("TenCNT"));
				thongTinNhaTro.setDiaChiCNT(rs.getString("DiaChiCNT"));
				thongTinNhaTro.setGioiTinhCNT(rs.getString("GioiTinhCNT"));
				thongTinNhaTro.setSdtCNT(rs.getString("SDTCNT"));
				thongTinNhaTro.setEmailCNT(rs.getString("EmailCNT"));
				thongTinNhaTro.setMaNT(rs.getInt("MaNT"));
				thongTinNhaTro.setDiaChiNT(rs.getString("DiaChiNT"));
				thongTinNhaTro.setGiaThueMotThang(rs.getFloat("GiaThueMotThang"));
				thongTinNhaTro.setMoTaNT(rs.getString("MoTaNT"));				
				listThongTinNhaTroEntity.add(thongTinNhaTro);
			}
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return listThongTinNhaTroEntity;
	}
	
	public int duyetNhaTro(int maNT) {
		String query = "UPDATE `nhatro` SET `TrangThaiDuyetNT` = 1 WHERE `MaNT` = ?";

		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = dataSource.getConnection();
			preparedStatement = connection.prepareStatement(query,
					Statement.RETURN_GENERATED_KEYS);
			preparedStatement.setInt(1, maNT);
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				connection.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return 0;
	}
}
